<?php
class ControllerBook extends ControllerMain implements iController
{
	protected $employee; //Employee object
	protected $appointment; //Appointment object
	protected $error; //Variable with error text
	protected $message; //Variable with message
	protected $book = array(); //Array with data for add appointment action
	
	public function index()
	{
		$this->viewForm();
	}		
	
	/*
	*View form for add appointment
	*/
	public function viewForm()
	{
		//Create data for options with employees
		$this->employee = new Employee();
		$this->keys['employees'] = $this->employee->getAllFields('id,name');
		if(empty($this->keys['employees']))
		{
			$this->keys['employees'][0]['id'] = '';
			$this->keys['employees'][0]['name'] = '';
		}
		//Create data for options with months
		for($i = 1; $i <= 12; $i++)
		{
			$this->keys['months'][$i]['numberMonth'] = $i;
			$this->keys['months'][$i]['titleMonth'] = date('M',mktime(0,0,0,$i + 1,0,0));
		}
		//Create data for options with days
		for($i = 1; $i <= 31; $i++)
		{
			$this->keys['days'][$i]['numberDay'] = $i;
		}
		//Create data for options with years
		for($i = date('Y',time()); $i <= date('Y',time()) + COUNT_YEARS; $i++)
		{
			$this->keys['years'][$i]['numberYear'] = $i;
		}
		//Create data for options with hours
		if(TIME_TYPE == 12)
		{
			$countHours = 12;
			$this->keys['timeType'] = 12;
		}
		elseif(TIME_TYPE == 24)
		{
			$countHours = 23;	
		}	
		for($i = 1; $i <= $countHours; $i++)
		{
			$this->keys['hours'][$i]['hour'] = $i;
		}
		//Create data for options with minutes
		for($i = 0; $i <= 59; $i++)
		{
			if($i == 0)
			{
				$this->keys['minutes'][$i]['minute'] = '00';
			}
			else
			{
				$this->keys['minutes'][$i]['minute'] = $i;
			}
		}
		//Set boardroom number
		$this->keys['number'] = BOARDROOM_NUMBER;
		//Send data to view
		$this->template = View::create($this->templateDir.'viewForm.html',$this->keys);
		View::assign('content',$this->template);
	}
	
	/*
	*Add new appointment action
	*/
	public function add()
	{
		$this->appointment = new Appointment(); 
		$this->validate(); //Call method to validate request data for appointment creation
		if(empty($this->error))
		{
			//Call model for create appointment
			if($this->appointment->addAppointment($this->book)) 
			{
				View::assign('content',View::message('Appointment successfully created'));
			}	
			else
			{
				View::assign('content',View::error('Time usually exists'));
			}
		}
		else
		{
			View::assign('content',$this->error);
		}	
		$this->viewForm();//View form for add new appointment
	}
	
	/*
	*Validate request data for add appointment action
	*
	*
	*return $this->book || $this->error
	*/
	private function validate()
	{
		if($_SERVER['REQUEST_METHOD'] == 'POST' && !empty($this->post['bookSubmit']))
		{
			if(!empty($this->post['employee']) &&
			   !empty($this->post['month']) && 
			   !empty($this->post['day']) &&
			   !empty($this->post['year']) &&
			   !empty($this->post['startHour']) &&
			   !empty($this->post['startMinute']) &&
			   !empty($this->post['endHour']) &&
			   !empty($this->post['endMinute']) &&
			   !empty($this->post['description']) &&
			   !empty($this->post['recurring']))
			{
				$this->book['idemployee'] = $this->clearData($this->post['employee'],'integer');
				if(empty($this->book['idemployee']))
				{
					$this->error .= View::error('Incorrect information about employee.');
				}
				$this->book['month'] = $this->clearData($this->post['month'],'integer');
				if($this->book['month'] > 12 || $this->post['month'] < 1)
				{
					$this->error .= View::error('Incorrect information about month');
				}
				$this->book['day'] = $this->clearData($this->post['day'],'integer');
				if($this->book['day'] > 31 || $this->post['month'] < 1)
				{
					$this->error .= View::error('Incorrect information about day');
				}
				$this->book['year'] = $this->clearData($this->post['year'],'integer');
				if($this->book['year'] < date('Y',time()) ||
				   $this->book['year'] > date('Y',time()) + COUNT_YEARS)
				{
					$this->error .= View::error('Incorrect information about year');
				}
				if(isset($this->post['startAMPM']) && isset($this->post['endAMPM']))
				{
					if($this->post['startHour'] >= 0 && $this->post['startHour'] <= 12)
					{
						if($this->post['startMinute'] >= 0 && $this->post['startMinute'] <=59)
						{
							if($this->post['startAMPM'] == 'pm')
							{
								if($this->post['startHour'] != 12)
								{
									$this->book['startHour'] = $this->post['startHour'] + 12;
								}
								else
								{
									$this->book['startHour'] = $this->post['startHour'];	
								}		
							}
							if($this->post['startAMPM'] == 'am')
							{
								if($this->post['startHour'] != 12)
								{
									$this->book['startHour'] = $this->post['startHour'];
								}
								else
								{
									$this->book['startHour'] = '0';	
								}	
							}
							$this->book['startMinute'] = $this->post['startMinute'];
						}
						else
						{
							$this->error .= View::error('Incorrect information about start time.');
						}
					}
					if($this->post['endHour'] >= 0 && $this->post['endHour'] <= 12)
					{
						if($this->post['endMinute'] >= 0 && $this->post['endMinute'] <=59)
						{
							if($this->post['endAMPM'] == 'pm')
							{
								if($this->post['endHour'] != 12)
								{
									$this->book['endHour'] = $this->post['endHour'] + 12;
								}
								else
								{
									$this->book['endHour'] = $this->post['endHour'];	
								}		
							}
							if($this->post['endAMPM'] == 'am')
							{
								if($this->post['endHour'] != 12)
								{
									$this->book['endHour'] = $this->post['endHour'];
								}
								else
								{
									$this->book['endHour'] = '0';	
								}	
							}
							$this->book['endMinute'] = $this->post['endMinute'];
						}
						else
						{
							$this->error .= View::error('Incorrect information about end time.');
						}
					}
					else
					{
						$this->error .= View::error('Incorrect information about end time.');	
					}
				}
				elseif(!isset($this->post['startAMPM']) && !isset($this->post['endAMPM']))
				{
					if($this->post['startHour'] >= 0 && $this->post['startHour'] <= 23)
					{
						$this->book['startHour'] = $this->post['startHour'];
						if($this->post['startMinute'] >= 0 && $this->post['startMinute'] <= 59 )
						{
							$this->book['startMinute'] = $this->post['startMinute'];
						}
						else
						{
							$this->error .= View::error('Incorrect information about start time.');
						}
					}
					else
					{
						$this->error .= View::error('Incorrect information about start time.');
					}
					if($this->post['endHour'] >= 0 && $this->post['endHour'] <= 23)
					{
						$this->book['endHour'] = $this->post['endHour'];
						if($this->post['endMinute'] >= 0 && $this->post['endMinute'] <= 59 )
						{
							$this->book['endMinute'] = $this->post['endMinute'];
						}
						else
						{
							$this->error .= View::error('Incorrect information about end time.');
						}
					}
					else
					{
						$this->error .= View::error('Incorrect information about end time.');
					}
				}
				else
				{
					$this->error .= View::error('Incorrect information about time of appointment!');
				}
				$this->book['description'] = $this->clearData($this->post['description'],'string');
				if(empty($this->book['description']))
				{
					$this->error .= View::error('Incorrect description.');	
				}
				
				$this->book['recurring'] = $this->clearData($this->post['recurring'],'string');
				if($this->book['recurring'] == 'yes')
				{
					if(!empty($this->post['recurringType']) && !empty($this->post['duration']))
					{
						$this->book['recurringType'] = $this->clearData($this->post['recurringType'],'string');
						if(empty($this->book['recurringType']))
						{
							$this->error .= View::error('Incorrect information about recurring type.');	
						}
						
						$this->book['duration'] = $this->clearData($this->post['duration'],'integer');
						if($this->book['duration'] <= 1 || $this->book['duration'] > 4)
						{
							$this->error .= View::error('Incorrect information about duration of appointment.');
						}
					}
					else
					{
						$this->error .= View::error('You don\'t enter all information about recurring option.');	
					}
				}
				if(empty($this->error))
				{
					$this->book['startUnixTime'] = mktime($this->book['startHour'],
														  $this->book['startMinute'],0,
														  $this->book['month'],
														  $this->book['day'],
														  $this->book['year']);
					$this->book['endUnixTime'] = mktime($this->book['endHour'],
														$this->book['endMinute'],0,
														$this->book['month'],
														$this->book['day'],
														$this->book['year']);
					if($this->book['startUnixTime'] >= $this->book['endUnixTime'])
					{
						$this->error .= View::error('You enter incorrect information about start and end time of appointment.');	
					}
					
					if($this->book['startUnixTime'] < time())
					{
						$this->error .= View::error('You enter incorrect information about date of appointment.');	
					}
				}	
			}
			else
			{
				$this->error = View::error('You don\'t enter all information.');
			}	
		}
		else
		{
			$this->error = View::error('Incorrect data!');
		}
	}
}